package com.xinruke.hostels.dailymanage.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xinruke.hostels.common.cache.ManageUserCache;
import com.xinruke.hostels.common.constants.SysDicConstant;
import com.xinruke.hostels.common.mybatis.MyExample;
import com.xinruke.hostels.common.po.YwFangjianleixing;
import com.xinruke.hostels.common.po.YwFangtaibiangengrizhi;
import com.xinruke.hostels.common.util.DateCalendarUtil;
import com.xinruke.hostels.common.vo.ResultVO;
import com.xinruke.hostels.common.cache.vo.LoginManageResultVO;
import com.xinruke.hostels.common.vo.query.EConditionOperator;
import com.xinruke.hostels.common.vo.query.QueryConditions;
import com.xinruke.hostels.common.vo.query.RowsDataVO;
import com.xinruke.hostels.dailymanage.interfaces.dto.roomstatuschange.RoomStatusChangeRecordAddDTO;
import com.xinruke.hostels.dailymanage.interfaces.dto.roomstatuschange.RoomStatusChangeRecordQueryDetailDTO;
import com.xinruke.hostels.dailymanage.interfaces.dto.roomstatuschange.RoomStatusChangeRecordQueryListDTO;
import com.xinruke.hostels.dailymanage.interfaces.dto.roomstatuschange.RoomStatusChangeRecordQueryResultDTO;
import com.xinruke.hostels.dailymanage.mapper.YwFangtaibiangengrizhiMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoomStatusChangeRecordService {

    @Autowired
    private YwFangtaibiangengrizhiMapper ywFangtaibiangengrizhiMapper;

    @Autowired
    private ManageUserCache manageUserCache;

    @Transactional(rollbackFor = Exception.class)
    public ResultVO add(RoomStatusChangeRecordAddDTO params) {
        ResultVO resultVO = ResultVO.success();

        LoginManageResultVO user = manageUserCache.getLoginManageUserInfo();

        YwFangtaibiangengrizhi add = new YwFangtaibiangengrizhi();
        BeanUtils.copyProperties(params, add);
        add.setJiudianId(user.getJiudianId());
        add.setChuangjianrenId(user.getId());
        add.setChuangjianshijian(new Date());
        add.setGengxinshijian(new Date());
        add.setShanchubiaoshi(SysDicConstant.DELFLAG_0);
        int insert = ywFangtaibiangengrizhiMapper.insertSelective(add);
        if(insert == 0){
            return ResultVO.fail();
        }
        return resultVO;
    }

    public ResultVO<RowsDataVO<RoomStatusChangeRecordQueryResultDTO>> list(RoomStatusChangeRecordQueryListDTO params) {

        if (null != params.getGengxinshijianleixing() && params.getGengxinshijianleixing() != DateCalendarUtil.CUSTOM) {
            params.setKaishishijian(DateCalendarUtil.getStartDate(params.getGengxinshijianleixing()));
            params.setJieshushijian(DateCalendarUtil.getEndDate(params.getGengxinshijianleixing()));
        }

        RowsDataVO<RoomStatusChangeRecordQueryResultDTO> dataVO = new RowsDataVO<>();
        QueryConditions conditions = new QueryConditions();

        conditions.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);
        conditions.addCondition("jiudianId", EConditionOperator.EQULS, manageUserCache.getJiudianId());
        conditions.addCondition("fangjianhao", EConditionOperator.LIKE, params.getFangjianhao());
        conditions.addCondition("kaishishijian", EConditionOperator.GREATER, params.getKaishishijian());
        conditions.addCondition("jieshushijian", EConditionOperator.LESS, params.getJieshushijian());

        MyExample example = new MyExample(YwFangtaibiangengrizhi.class, conditions, params.getOrderFields());
        Page<YwFangtaibiangengrizhi> page = PageHelper.startPage(params.getPageNo(), params.getPageSize());
        List<YwFangtaibiangengrizhi> list = ywFangtaibiangengrizhiMapper.selectByExample(example);

        List<RoomStatusChangeRecordQueryResultDTO> dtoList = list.stream().map(n -> {
            RoomStatusChangeRecordQueryResultDTO dto = new RoomStatusChangeRecordQueryResultDTO();
            BeanUtils.copyProperties(n, dto);
            return dto;
        }).collect(Collectors.toList());

        dataVO.setPageNo(page.getPageNum());
        dataVO.setTotalRecords(page.getTotal());
        dataVO.setTotalPages(page.getPages());
        dataVO.setRows(dtoList);
        return ResultVO.success("查询成功", dataVO);
    }

    public ResultVO<RoomStatusChangeRecordQueryResultDTO> detail(RoomStatusChangeRecordQueryDetailDTO params) {

        RoomStatusChangeRecordQueryResultDTO dto  = new RoomStatusChangeRecordQueryResultDTO();
        YwFangtaibiangengrizhi detail = ywFangtaibiangengrizhiMapper.selectByPrimaryKey(params.getId());

        if (null == detail) {
            return ResultVO.fail("没有此记录");
        }
        BeanUtils.copyProperties(detail, dto);
        return ResultVO.success("获取详情成功", dto);
    }
}
